package org.opentcs.kcvehicle.communication.kc.syn;


import org.opentcs.kcvehicle.communication.kc.common.CaffeineUtil;
import org.opentcs.kcvehicle.communication.kc.common.Package;

public class SendedList {
//    private static HashMap<String, AsyncFuture<MBPackage>> list = new HashMap<>();
//    public static synchronized AsyncFuture<MQMessage> get(String transitionId){
//        return list.get(transitionId);
//    }
//    public static synchronized void put(String transitionId,AsyncFuture<MQMessage> asyncFuture){
//        list.put(transitionId,asyncFuture);
//    }

  public static synchronized AsyncFuture<Package> add(String transitionId, Package PackageRequest) {
    AsyncFuture<Package> objectAsyncFuture = new AsyncFuture<>(PackageRequest);
    //list.put(transitionId,objectAsyncFuture);
    CaffeineUtil.put(transitionId, objectAsyncFuture);
    return objectAsyncFuture;
  }

  //如果超时了，那么让外部把这个key 给清除掉，防止 list 过大
  public static synchronized void remove(String transitionId) {
    //list.remove(transitionId);
    CaffeineUtil.remove(transitionId);
  }

  @SuppressWarnings("unchecked")
  public static synchronized Boolean set(String transitionId, Package message) {
    //AsyncFuture<MBPackage> mqMessageAsyncFuture = list.get(transitionId);
    AsyncFuture<Package> mqMessageAsyncFuture = (AsyncFuture<Package>) CaffeineUtil.get(
        transitionId
    );
    if (mqMessageAsyncFuture == null) {
      return false;
    }
    mqMessageAsyncFuture.done(message);
    //清除 ，防止这个hashMap过大。
    CaffeineUtil.remove(transitionId);
    return true;
  }


}
